/*
 * Login.fx
 * Author: Yu Min-ting
 * Created on 2009-7-9, 12:58:25
 */

package com.sjtu.fx.user;

import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.scene.input.MouseEvent;
import javafx.io.http.*;
import java.io.*;
import javafx.scene.effect.*;

import com.sjtu.fx.tools.*;

import javafx.stage.Stage;
import org.json.JSONObject;

import javafx.ext.swing.SwingLabel;


import java.lang.System;

import com.sjtu.fx.admin.AdminControlPanel;



public class Login extends CustomNode {
    var panel:Panel;
   
    public var username:String= bind userTextBox.text;
    public var password:String = bind passwdTextBox.text;
    public var userid:Integer on replace{System.out.println("changed: {userid }");};
    public var isAdmin:Boolean;
    var userText = Text {
        x: 25 y: 70
        font: Font {size:12 name: "Verdana Bold"}
        fill: Color.rgb(11,51,60)
        content: "User ID:"
    };

    var userTextBox = TextBox {
        translateX: 120
        translateY: 55
        width: 140
        blocksMouse:false
    }

    var passwdText = Text {
        x: 25 y: 95
       font: Font {size:12 name: "Verdana Bold"}
        fill: Color.rgb(11,51,60)
        content: "Password:"
        textOrigin: TextOrigin.TOP
    };

    var passwdTextBox : SwingPasswordField = SwingPasswordField {
        translateX: 120
        translateY: 90
        width: 140
        blocksMouse:false
        editable:true
    };
    
    var rememberPw : CheckBox = CheckBox {
        translateX:100 translateY: 130
        text: "Remember Password"
        font: Font {size:12 name: "Verdana Bold"}
        allowTriState: false
        selected: false
    }
    var logOn : Button = Button {
        translateX:25 translateY: 170
        text: "Log On"
        font: Font {size:11 name: "Verdana Bold"}
        action: function() {
            sendHttp();
        }
    }

    var register : Text = Text{
         content: "Register Now"
        fill: Color.rgb(11,51,60)
         font: Font{ size:13 name: "Verdana Bold"}
         x: 145 y: 185

         onMouseEntered: function( e: MouseEvent ):Void {
                 register.fill =  Color.BLUE;
                 register.effect = Reflection {
                fraction: 0.75
                topOffset:-1.0
            }
             }
         onMouseExited: function(e:MouseEvent) {
                register.fill = Color.rgb(11,51,60);
                register.effect = null;
        }
        onMouseClicked: function( e: MouseEvent ):Void {
                this.parent.lookup("topBooks").visible = false;
                this.parent.lookup("recommendBooks").visible = false;
                this.parent.lookup("register").visible = true;
        }
    }
   var loginError:SwingLabel = SwingLabel {
        text: " "
        foreground: Color.rgb(255,0,0);
         font: Font{ size:13 name: "Verdana Bold"}
    //     verticalAlignment : SwingVerticalAlignment.CENTER;
            width:250
         layoutX: 20
         layoutY: 200
    }

    override protected function create() : Node {
        Group{
            content:[
                 panel = Panel {
                    width:    290
                    height:   240
                    title:    "Members Login"
                    translateX: 0
                    translateY: 0
                },
                userText , userTextBox ,
                passwdText ,passwdTextBox,
                rememberPw , logOn , register,loginError
            ]
        }
     }

function run() {
    Stage {
       width: 350 height: 300
        scene: Scene {
            content: [
                Login {translateX: 40 translateY: 40}
            ]
        }
    }
}

function sendHttp(){
    HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/Login"

       onOutput: function(os:OutputStream) {
            try {
                var json:JSONObject = JSONObject{};
                json.put("username",username);
                json.put("password",password);
                var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }

        onInput: function(is: InputStream) {
            try {
                def data:DataInputStream = new DataInputStream(is);
                var json:JSONObject = JSONObject{};
                var result:String = data.readLine();
                if(result.equals("Wrong combination of username and password"))
                {
                    this.passwdTextBox.text = "";
                    this.userTextBox.text = "";
                    loginError.text = "username or password error!";
                }else{
                json = new JSONObject(result);
                userid = json.getInt("id");
                isAdmin = json.getBoolean("isAdmin");
                var userName:String = json.getString("name");
                if(isAdmin){
                    var admin:AdminControlPanel  = parent.lookup("adminPanel") as AdminControlPanel;
                    admin.helloName = "welcome, {userName}";
                    this.visible = false;
                    this.parent.lookup("adminPanel").visible = true;
                }else{
                    var user:UserControlPanel  = parent.lookup("userPanel") as UserControlPanel;
             //       System.out.println("cao: {userName}");
                //    System.out.println(user.helloName);
               //     System.out.println(userName);
                    user.helloName = "Hello, {userName}";
                   // var userText:Text = user.lookup("welcomeText") as Text;
                  //  System.out.println("gogogo{userText.content}");
             //       System.out.println(userText.translateX);
             //       System.out.println("content Here:{((parent.lookup('userPanel') as UserControlPanel).lookup("welcomeText") as Text).translateX}");
             //       System.out.println("content Here:{((parent.lookup('userPanel') as UserControlPanel).lookup('welcomeText') as Text).content}");
              //      (parent.lookup("userPanel").lookup("welcomeText") as Text).content = "welcome, {userName}";
                    this.visible = false;
                    this.parent.lookup("userPanel").visible = true;
                }
                }
        /*        var json:JSONObject = new JSONObject(data.readLine());
                var isAdmin:Boolean = json.getBoolean("isAdmin");
                this.visible = isAdmin;
                var userId:String = json.getString("id");
            //    json.put("username",username);
            //   json.put("password",password);
             //   System.out.println(isAdmin);
             //   System.out.println(userId);
    */         // is.flush();
            } finally {
                is.close();
            }
        }
    }.start();
}

}
